Guide to NoSQL with Azure Cosmos DB by Gastón C. Hillar
Author:Gastón C. Hillar
Language: eng
Format: epub
Tags: COM021000 - COMPUTERS / Databases / General, COM060080 - COMPUTERS / Web / General, COM021050 - COMPUTERS / Databases / Servers
Publisher: Packt Publishing
Published: 2018-09-28T06:35:51+00:00
Querying and creating document collections
The following lines declare the code for the CreateCollectionIfNotExistsAsync asynchronous static method, which creates a new document collection if a collection with id equal to the value stored in the collectionId field doesn't exist in the database. Add the following lines to the existing code of the Program.cs file. The code file for the sample is included in the learning_cosmos_db_04_01 folder in the dot_net_core_2_samples/SampleApp1/SampleApp1/Program.cs file:
private static async Task<DocumentCollection> CreateCollectionIfNotExistsAsync() { var databaseUri = UriFactory.CreateDatabaseUri(databaseId); DocumentCollection documentCollectionResource; var isCollectionCreated = await client.CreateDocumentCollectionQuery(databaseUri) .Where(c => c.Id == collectionId) .CountAsync() == 1; if (isCollectionCreated) { Console.WriteLine($"The collection {collectionId} already exists."); var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId); var documentCollectionResponse = await client.ReadDocumentCollectionAsync(documentCollectionUri); documentCollectionResource = documentCollectionResponse.Resource; } else { var documentCollection = new DocumentCollection { Id = collectionId, }; documentCollection.PartitionKey.Paths.Add("/location/zipCode"); var uniqueKey = new UniqueKey(); uniqueKey.Paths.Add("/title"); documentCollection.UniqueKeyPolicy.UniqueKeys.Add(uniqueKey); var requestOptions = new RequestOptions { OfferThroughput = 1000, }; var collectionResponse = await client.CreateDocumentCollectionAsync( databaseUri, documentCollection, requestOptions); if (collectionResponse.StatusCode == System.Net.HttpStatusCode.Created) { Console.WriteLine($"The collection {collectionId} has been created."); } documentCollectionResource = collectionResponse.Resource; } return documentCollectionResource; }
The code doesn't use the easiest mechanism to create a collection when it doesn't exist because we will analyze how we can query the document collections for a database. Our goal is to learn about many possibilities offered by the SDK that will enable us to develop many different kinds of applications that work with Cosmos DB. However, it is very important to notice that the code for this method could be simplified by calling the CreateDocumentCollectionIfNotExistsAsync method.
First, the code calls the UriFactory.CreateDatabaseUri method with databaseId as its argument and saves the result in the databaseUri variable. This method will return a Uri instance with the URI for the database ID received as an argument. The code will use this URI to easily address the database resource in which we have to perform operations.
Note that the UriFactory.CreateDatabaseUri method requires the database ID to build the Uri instance and doesn't require any query to the database. However, of course, we must be sure that the database resource with the specified ID already exists before using the generated URI.
The next line declares the documentCollectionResource variable as a DocumentCollection instance. The code will end up returning this variable.
Then, the code creates a LINQ query with a call to the asynchronous client.CreateDocumentCollectionQuery method with databaseUri as an argument. This counts the number of collections whose Id is equal to collectionId with an asynchronous execution due to the usage of the chained CountAsync method. If the results of this query on the collections for the database is 1, it means that the collection already exists.
The following lines show the code that builds the LINQ query and stores the results of the Boolean expression in the isCollectionCreated variable:
var isCollectionCreated = await client.CreateDocumentCollectionQuery(databaseUri) .Where(c => c.Id == collectionId) .CountAsync() == 1;
We can easily query the existing collections in a document database by chaining LINQ expressions to the call to the CreateDocumentCollectionQuery method. In this case, we are always working with asynchronous methods. If we used the Count method instead of CountAsync, the query would have a synchronous execution.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27094)
Hello! Python by Anthony Briggs(25942)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25285)
Kotlin in Action by Dmitry Jemerov(24393)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23311)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21943)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20847)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17072)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16832)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12581)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10650)
Hit Refresh by Satya Nadella(9236)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8445)
Robo-Advisor with Python by Aki Ranin(8387)